package com.xiaoxi.userserver.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xiaoxi.common.constant.enumeration.statuscode.user.RoleUserEnums;
import com.xiaoxi.common.exception.BMSException;
import com.xiaoxi.domain.user.BMSUserRole;
import com.xiaoxi.userserver.mapper.user.BMSUserRoleMapper;
import com.xiaoxi.userserver.service.role.BMSRoleService;
import com.xiaoxi.userserver.service.user.BMSUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName BMSUserRoleServiceImpl
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/3 17:39
 * @Version 1.0
 */
@Service
@Slf4j
public class BMSUserRoleServiceImpl extends ServiceImpl<BMSUserRoleMapper, BMSUserRole> implements BMSUserRoleService {

    @Resource
    private BMSUserRoleMapper bmsUserRoleMapper;

    @Resource
    private BMSRoleService bmsRoleService;

    @Override
    public List<Long> getUserRoleListByUserId(Long userId) {
        QueryWrapper<BMSUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Long> roleIds = new ArrayList<>();
        bmsUserRoleMapper.selectList(queryWrapper).forEach(bmsUserRole -> {
            roleIds.add(bmsUserRole.getRoleId());
        });
        return roleIds;
    }

    @Override
    public void deleteUserRoleListByUserId(Long userId) {
        UpdateWrapper<BMSUserRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        bmsUserRoleMapper.delete(updateWrapper);
    }

    @Override
    public void deleteUserRoleByRoleId(Long roleId) {
        UpdateWrapper<BMSUserRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", roleId);
        bmsUserRoleMapper.delete(updateWrapper);
    }

    @Override
    public void addUserRoleListByUserId(Long userId, List<Long> roleIds) throws BMSException {

        if (Objects.isNull(roleIds) || roleIds.size() == 0) {
            log.warn("{} ： 未传对应的角色编号", userId);
            return;
        }

        // 校验 roleIds 是否正确
        boolean check = bmsRoleService.checkRoleIdListValid(roleIds);

        if(!check) {
            throw new BMSException(RoleUserEnums.INCORRECT_ROLE_ID);
        }

        List<BMSUserRole> bmsUserRoleList = new ArrayList<>(roleIds.size());

        for (Long roleId : roleIds) {
            BMSUserRole bmsUserRole = new BMSUserRole();
            bmsUserRole.setUserId(userId);
            bmsUserRole.setRoleId(roleId);
            bmsUserRoleList.add(bmsUserRole);
        }

        saveBatch(bmsUserRoleList);

    }

    @Override
    public void updateUserRoleList(Long userId, List<Long> roleIds) throws BMSException {
        deleteUserRoleListByUserId(userId);
        addUserRoleListByUserId(userId, roleIds);
    }

}
